package Route;

import java.util.Scanner;

import Helper.*;
import View.*;

public class Router {

    public static void jmper(String now){
        System.out.print("请输入菜单编号: ");
        switch( now ){
            case "index":
                switch( getInputNum(1, 5) ){
                    case 1:
                        switch( Index.login() ){
                            case 0:
                                UserIndex.index(Helper.getSession(0));
                                Router.jmper("userIndex");
                            break;
                            case 1:
                                VendorIndex.index(Helper.getSession(0));
                                Router.jmper("vendorIndex");
                            break;
                            case 2:
                                AdminIndex.index(Helper.getSession(0));
                                Router.jmper("adminIndex");
                            break;
                        }
                        break;
                    case 2:
                        Index.signup();
                        break;
                    case 3:
                        Index.listProducts();
                        break;
                    case 4:
                        Index.searchProduct();
                        break;
                    case 5:
                        Helper.exit();
                }
                break;
            case "userIndex":
                switch ( getInputNum(1, 5) ){
                    case 1:
                        UserIndex.listProducts();
                        break;
                    case 2:
                        UserIndex.searchProduct();
                        break;
                    case 3:
                        UserIndex.listOrders();
                        break;
                    case 4:
                    case 5:
                        return;
                }
                UserIndex.index(Helper.getSession(0));
                Router.jmper("userIndex");
                break;
            case "vendorIndex":
                switch ( getInputNum(1, 6) ){
                    case 1:
                        VendorIndex.listProducts();
                        break;
                    case 2:
                        VendorIndex.searchProduct();
                        break;
                    case 3:
                        VendorIndex.addProduct();
                        break;
                    case 4:
                        VendorIndex.editProduct();
                        break;
                    case 5:
                        VendorIndex.removeProduct();
                        break;
                    case 6:
                        return;
                }
                VendorIndex.index(Helper.getSession(0));
                Router.jmper("vendorIndex");
                break;
            case "adminIndex":
                getInputNum(1,2);
                AdminIndex.index(Helper.getSession(0));
                Router.jmper("adminIndex");
                break;
        }
    }

    private static int getInputNum(int min, int max){
        Scanner scanner = new Scanner(System.in);

        int input = max;
        boolean status = false;
        do{
            String temp = scanner.next();
            if( temp.matches("[0-9]") ){
                input = Integer.parseInt(temp);

                if( input<min || input>max ){
                    status = true;
                }else{
                    status = false;
                }
            }else{
                status = true;
                System.out.print("非法输入，请重新输入: ");
            }
        }while( status );
        Helper.clear();

        return input;
    }
}
